package com.scalar.chiptrack.jasper.base;

import net.sf.jasperreports.engine.util.FileResolver;

import java.io.File;

/**
 * utility class to load the jrtx files. This class will locate the absolute path from the context given the relative or absolute path of the jrtx file.
 */
public class JasperFileResolver implements FileResolver {
	private String absoluteContextPath = null;

	public JasperFileResolver(String absoluteContextPath) {
		this.absoluteContextPath = absoluteContextPath;
	}

	public File resolveFile(String fileName) {
		if(fileName != null && fileName.toLowerCase().endsWith("jrtx")) {
			//find the context absolute path
			int relativePathIdx = fileName.indexOf("jasper_reports");
			if(relativePathIdx >= 0) {
				File file = new File (absoluteContextPath + File.separator + fileName.substring(relativePathIdx));
				if (file.exists() && file.isFile()) {
					return file;
				}
			}
		}
		return null;
	}
}
